<template>
    <div class="pageMain">

        <img src="../../assets/login/yuan.png" class="yuanImg" alt="">

        <img src="../../assets/login/logo.png" class="logoImg" alt="">

        <img src="../../assets/login/tip.png" class="tipImg" alt="">


        <img src="../../assets/login/boLang.png" class="boLangImg" alt="">

        <div class="upAlert" v-if="tipFlag">
            <div class="tipTitle">本次更新内容：</div>
            <div class="tipNote">{{ version_note }}</div>
        </div>
    </div>
</template>

<script>

import { listNewVer } from '../../network/index'

export default {
    name: 'HomeR',
    data() {
        return {
            version_note: '',
            tipFlag: false,
            wgtUrl: "https://zbz-downlink.oss-cn-zhangjiakou.aliyuncs.com/H5EDF0742.wgt",// 下载地址
        }
    },
    mounted() {

    },
    methods: {
        getData() {
            listNewVer().then(res => {
                var that = this;
                if (res.flag) {
                    var versions = res.data[0];
                    localStorage.version = versions.versionCode;
                    if (versions.versionCode != ver) {
                        that.tipFlag = true
                        that.version_note = versions.versionNote
                        var _msg = "发现新版本:V" + versions.versionCode + ";共" + versions.versionSize + 'MB';
                        that.$dialog.confirm({
                            title: '智医助手',
                            message: _msg,
                            confirmButtonText: '确认升级',
                        })
                            .then(() => {
                                // 升级
                                plus.nativeUI.toast("正在准备环境，请稍后！");
                                that.downWgt();
                            })
                            .catch(() => {
                                // 暂不升级
                                if (phone_system == 'iOS') {
                                    const threadClass = plus.ios.importClass("NSThread");
                                    const mainThread = plus.ios.invoke(threadClass, "mainThread");
                                    plus.ios.invoke(mainThread, "exit");
                                } else {
                                    plus.runtime.quit();
                                }
                            });
                    } else {
                        this.$router.replace('/wxLogin')
                    }

                }
            })

        },
        downWgt: function () {
            var that = this
            var w = plus.nativeUI.showWaiting("下载文件中...");
            dtask = plus.downloader.createDownload(that.wgtUrl, {
                filename: "_doc/update/"
            });
            dtask.start();
            dtask.addEventListener("statechanged", function (task, status) {
                switch (task.state) {
                    case 1: // 开始
                        w.setTitle("开始下载...");
                        break;
                    case 2: // 已连接到服务器
                        w.setTitle(" 开始下载...");
                        break;
                    case 3:
                        var a = task.downloadedSize / task.totalSize * 100;
                        w.setTitle("已下载" + parseInt(a) + "%");
                        break;
                    case 4: // 下载完成
                        w.close();
                        that.installWgt(task.filename); // 安装wgt包
                        break;
                }
            });
        },
        installWgt: function (path) {
            plus.nativeUI.showWaiting("正在安装中...");
            // force:false进行版本号校验，如果将要安装应用的版本号不高于现有应用的版本号则终止安装，并返回安装失败  
            plus.runtime.install(path, {
                force: false
            }, function () {
                plus.nativeUI.closeWaiting();
                plus.nativeUI.alert("更新完成！", function () {
                    plus.runtime.restart();
                });
            }, function (e) {

                plus.nativeUI.closeWaiting();
                plus.nativeUI.alert("本次更新失败，请卸载现有APP重新下载", function () {
                    if (phone_system == "iOS") {
                        window.location.replace("https://apps.apple.com/cn/app/%E5%80%BC%E7%8F%AD%E9%95%BF/id1448441059");
                    } else {
                        window.location.replace("https://a.app.qq.com/o/simple.jsp?pkgname=com.ccclkj.clinic");
                    }
                }, "提示", "点击下载");
            });
        }



    },
    created() {
        this.getData()

    },
}

var ver = null, verCode = null, phone_system = null, dtask = null;

plus.runtime.getProperty(plus.runtime.appid, function (inf) {
    ver = inf.version;
    verCode = inf.versionCode;
    phone_system = plus.os.name;
});



</script>

<style lang='less' scoped>
.pageMain {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
    background: linear-gradient(111deg, #14A870, #20BB66);
}

.upAlert {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 80%;
    background: rgba(255, 255, 255, .6);
    border-radius: 16px;
    padding: 16px 24px;
    font-size: 28px;
    color: #333;
    overflow: hidden;
    color: black;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

.tipTitle {
    text-align: center;
    color: #333;
}

.tipNote {
    text-align: left;
    line-height: 60px;
}


.yuanImg {
    position: absolute;
    top: 0px;
    right: 0px;
    width: 446px;
    height: 376px;
}


.logoImg {
    width: 298px;
    height: 350px;
}

.tipImg {
    margin-top: 28vh;
    width: 300px;
    height: 184px;
}

.tipBtn {
    margin-top: 88px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 400px;
    height: 90px;
    border-radius: 44px;
    border: 1px solid #FFFFFF;
    font-family: Source Han Sans CN;
    font-weight: 500;
    font-size: 36px;
    color: #FFFFFF;
    line-height: 48px;
}

.boLangImg {
    position: absolute;
    bottom: 0px;
    left: 0px;
    width: 750px;
    height: 146px;
}
</style>